package web.comment;

import bean.Comment;
import bean.User;
import bean.Video;
import com.alibaba.fastjson.JSON;
import entity.CommentEntity;
import entity.TypeEntity;
import entity.UserEntity;
import entity.VideoEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "CommentEdit",urlPatterns = "/dao/update/ccc")
public class CommentEdit extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonString = FANREN.getJson(request);
        Comment comment = JSON.parseObject(jsonString,Comment.class);
        User user = (User)request.getSession().getAttribute("user");
        if(user!=null) {
            EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
            CommentEntity c = new CommentEntity();
            UserEntity userEntity = em.find(UserEntity.class,Integer.parseInt(user.getUid()));
            VideoEntity videoEntity = em.find(VideoEntity.class,Integer.parseInt(comment.getVid()));
            c.setContent(comment.getContent());
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            c.setDate(sf.format(new Date( )));
            c.setUid(userEntity);
            c.setVid(videoEntity);
            c.setName(userEntity.getName());
            em.persist(c);
        }
    }
}
